$(function () {

    $("#xsd_sszje_into").html(formatJeForHtml2($("#xsd_shzje").val()));

    $("#sdxk_table").datagrid({
        url: $.webPath() + '/xsgl/xsdgl/sdxk_sfkList?xsdId=' + $("#xsd_id").val(),
        width: 1700,
        // height: 2400,
        toolbar: "#sdxk_toolbar",
        fit: true,
        fitColumns: true,
        singleSelect: false,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter: true,
        rownumbers: true,
        columns: [
            [
                {field: 'checked', checkbox: true},
                {field: 'id', title: '收款单号ID', hidden: true, width: 100, align: 'center'},
                {field: 'bm', title: '收款单号', width: 100, align: 'center'},
                {field: 'sfje', title: '收款金额（元）', width: 100, align: 'center', formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }},
                {field: 'ye', title: '收款余额（元）', width: 100, align: 'center', formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }},
                {field: 'khfksj', title: '客户付款日期', width: 100, align: 'center'},
                {field: 'sfkrq', title: '收款日期', width: 100, align: 'center'},
                {field: 'fkfs.z', title: '付款方式', width: 60, align: 'center'},
                {field: 'kxsy', title: '款项用途', width: 100, align: 'center'},
                {field: 'fplx', title: '款项类型', width: 100, align: 'center', formatter: function (value) {
                    if (value == 0) {
                        return "非计划现货";
                    } else {
                        return "金融物流";
                    }
                }}
//                {field: 'tx', title: '贴息', width: 100, align: 'center'},
//                {field: 'txye', title: '贴息余额', width: 100, align: 'center'}
            ]
        ]
    })


    $("#xsd_xsdmx_table").datagrid({
        url: $.webPath() + '/xsgl/xsdgl/xsdgl_xsmx?xsdId=' + $("#xsd_id").val(),
        width: 1700,
        fitColumns: false,
        singleSelect: true,
        fit: true,
        showFooter: true,
        rownumbers: true,
        columns: [
            [
                // {field:'wzgg.oldcode', title:'老物资编码', align:'center', width:150},
                {field: 'xxsdh', title: '销售合同子项号', width: 150, align: 'center'},
                {field: 'wzgg.name', title: '物资名称', width: 100, align: 'center'},
                {field: 'wzgg.cz', title: '材质', width: 150, align: 'center'},
                {field: 'wzgg.ggxh', title: '规格型号', width: 100, align: 'center'},
                {field: 'carNo', title: '车皮号', width: 100, align: 'center'},
                {field: 'bindNo', title: '捆包号', width: 100, align: 'center'},
                {field: 'wzgg.th', title: '图号', hidden: true, align: 'center'},
                {field: 'ck', title: '长宽', hidden: true, align: 'right'},
                {field: 'wzgg.dw', title: '单位', width: 50, align: 'center'},
                //{field: 'cd.z', title: '产地', width: 50},
                {field: 'jzfs.z', title: '记重方式', width: 50, align: 'center'},
                {field: 'kh.khbm', title: '卡号', width: 120, align: 'center'},
                {field: 'xscb', title: '销售价(元)', width: 120, align: 'right', formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }},
                {field: 'js', title: '件数', width: 50, align: 'right'},
                {field: 'sbsl', title: '重量', width: 100, align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }},
                {field: 'hsj', title: '含税价(元)', width: 150, align: 'right',
                    formatter: function (value) {
                        var sl = formatJeForHtml(value);
                        return sl;
                    } },
                {field: 'hse', title: '含税额(元)', width: 150, align: 'right',
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    } },
                {field: 'bhsj', title: '不含税价(元)', width: 150, align: 'right',
                    formatter: function (value) {
                        var sl = formatJeForHtml(value);
                        return sl;
                    } },
                {field: 'bhse', title: '不含税额(元)', width: 150, align: 'right',
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    } },
                {field: 'xscbzh', title: '销售成本', width: 150, align: 'right',
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    } },
                {field: 'xslr', title: '销售利润', width: 150, align: 'right',
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    } },

                {field: 'sjdj', title: '实际单价', width: 150, align: 'right',
                    formatter: function (value) {
                        var sl = formatJeForHtml(value);
                        return sl;
                    } },

                {field: 'cgd.cgdh', title: '采购单号', align: 'center', formatter: function (value, row) {
                    if (row['cgd.id']) {
                        return "<a style='color:red' href='javascript:void(0);' onclick='viewXsdglCgdDetail(" + row['cgd.id'] + ")'>" + value + "</a>";
                    } else
                        return value;
                }, width: 100},
                {field: 'jsdh', title: '结算单号', align: 'center', width: 100, formatter: xsdToXsjsdCell}

            ]
        ],
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.hsj) {
                    this.hse = Math.round(multiply(this.sbsl, this.hsj) * 100) / 100;
                }
                if (this.bhsj) {
                    this.bhse = Math.round((this.hse / (1 + $("#xsd_detail_sl").val() / 100)) * 100) / 100;
                }
            })
            return data;
        },
        onLoadSuccess: function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            var data = $("#xsd_xsdmx_table").edatagrid('getData');
            $.map(data.rows, function (n, i) {
                $("[datagrid-row-index='" + i + "']").children("td[field='hsj']").tooltip({
                    position: 'top',
                    content: '<span style="color:#ff0000">' + n['jsfs'] + '</span>'
                })
            })
        }
    })

})

/**
 * 计算当前代理费
 * @param xsdId
 */
function calDlfAndTx(xsdId) {
    var record = $("#sdxk_table").edatagrid("getChecked");
    var sfkIds = "";
    var zdlf = $("#zdlf").val();
    var swjsdlf = $("#wjsdlf").val();
    if (record && record.length > 0) {
        for (var i = 0; i < record.length; i++) {
            if (record[i].kxsy != '货款' && record[i].kxsy != '合同保证金') {
                $("sdxk_table").mask('hide');
                $.messager.alert("提示", "请勾选要挂的货款和合同保证金!", "waring");
                return false;
            }
            sfkIds += record[i].id + ",";
        }
        $.ajax({
            type: "POST",
            url: $.webPath() + '/xsgl/xsdgl/jsDlfTx?sfkIds=' + sfkIds + '&xsdId=' + xsdId,
            dataType: "html",
            beforeSend: function () {
                $.messager.progress();
            },
            success: function (data) {
                $.messager.progress("close");
                var obj = eval('(' + data + ')');
                if (obj.statusCode == '200') {
                    $("#dqdlf").html(formatJeForHtml2(obj.dqdlf));
                    $("#szdlf").html(formatJeForHtml2(parseFloat(obj.dqdlf) + parseFloat(zdlf)));
                    $("#swjsdlf").html(formatJeForHtml2(parseFloat(obj.dqdlf) + parseFloat(swjsdlf)));
                } else {
                    $.messager.alert("提示", obj.message, "waring");
                }
            }
        });
    } else {
        $.messager.alert("提示", "请勾选要挂的货款和合同保证金!", "waring");
    }

}

function viewXsdglXshtDetail(id) {
    $.window({
        iconCls: 'icon-tip',
        title: "详细信息",
        url: $.webPath() + "/htgl/xshtgl/xshtgl_detail?xshtId=" + id,
        width: 1050,
        height: 500,
        modal: true,
        isMax: false
    })
}




